import type { ResponseData } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import { request } from 'umi';
import type { activityItem, createactivityItem, QueryParams, updateactivityItem } from './types';

//查询单个数据
export async function getActivityDetail(id: string) {
  const res = await request(`/app/news/${id}`);
  return res;
}

//活动列表
export async function getAcitvitytable(params: QueryParams): Promise<RequestData<activityItem>> {
  const { current, ...param } = params;
  const res = await request('/app/news', {
    params: {
      page: params.current,
      pageSize: params.pageSize,
      ...param,
    },
  });

  return {
    current: res.data.current,
    success: true,
    total: res.data.pagination.total,
    data: res.data.list,
  };
}

//删除活动
export async function deleteAcitvity(params: { id: string }): Promise<void> {
  await request(`/app/news/${params.id}`, {
    method: 'DELETE',
  });
}

//添加活动
export async function addAcitvity(params: createactivityItem) {
  const res: ResponseData<activityItem> = await request('/app/news', {
    method: 'POST',
    data: {
      ...params,
    },
  });
  return res;
}

//修改活动
export async function updateAcitvity(id: string, params: updateactivityItem) {
  const res: ResponseData<activityItem> = await request(`/app/news/${id}`, {
    method: 'PUT',
    data: {
      id: id,
      ...params,
      address: params.address || '-',
    },
  });
  return res;
}
